var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
// 引入数据库
require("./utils/MDB");


// var indexRouter = require('./routes/index');
// var usersRouter = require('./routes/users');
var errorRouter = require('./routes/error');

var titleRouter = require('./routes/title');
var formalTestRouter = require('./routes/formalTest');
var subjectRouter = require('./routes/subject');


var collectionRouter = require('./routes/collection');
var wrongTopicRouter = require('./routes/wrongTopic');
// var indexRouter = require('./routes/index');
// var usersRouter = require('./routes/users');

var userRouter = require('./routes/user');//引入登录login的js文件

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


// app.use('/', indexRouter);
app.use('/error', errorRouter);



// app.use('/users', usersRouter);
app.use('/title', titleRouter);
app.use('/formalTest', formalTestRouter);
app.use('/subject', subjectRouter);

// 进入我们路由配置前进行身份验证
const jwtAuth = require("./utils/auth.config.js")
// 在进访问之前验证身份
app.use(jwtAuth)

app.use('/collection', collectionRouter);
app.use('/wrongTopic', wrongTopicRouter);
// app.use('/', indexRouter);
// app.use('/users', usersRouter);



app.use('/user',userRouter);//设置登录一级路由

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});


app.listen(3000,()=>{
  console.log("服务器已启动，端口号为3000");
})


